package framework.view {
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;

	/**
	 * @author Charlie MacIsaac
	 */
	
	public class BlitDisplayObjectView {
		// PROPERTIES =============================================================================
		private var _name : String;
		private var _canvas : BlitCanvasView;
		
		private var _sprites : Array;
		private var _spriteRectangle : Rectangle;
		
		private var _position : Point;
		private var _rotation : Number;
		
		private var _currentFrame : uint;
		
		// GETTERS + SETTERS ======================================================================
		public function set x(value : Number) : void { _position.x = value; }
		public function set y(value : Number) : void { _position.y = value; }
		
		public function set rotation(value : Number) : void { _rotation = value; }
		public function get rotation() : Number { return _rotation; }
		
		public function get x() : Number { return _position.x; }
		public function get y() : Number { return _position.y; }
		
		public function get height() : Number { return _spriteRectangle ? _spriteRectangle.height : 0; }
		public function get width() : Number { return _spriteRectangle ? _spriteRectangle.width : 0; }
		
		public function get name() : String { return _name; }
		
		public function set canvas(blitCanvas : BlitCanvasView) : void { _canvas = blitCanvas; }
		
		private function set currentFrame(id : uint) : void {
			if (id >= _sprites.length) {
				trace("[BlitDisplayObjectView.currentFrame] |WARN| Supplied ID is out of range : " + _sprites.length);
				id = _sprites.length - 1;
			}
			_currentFrame = id;
			
			var spriteBitmapData : BitmapData = _sprites[_currentFrame];
			_spriteRectangle.width = spriteBitmapData.width;
			_spriteRectangle.height = spriteBitmapData.height;
		}
		
		// CONSTRUCTOR ============================================================================
		public function BlitDisplayObjectView(name : String) {
			_name = name;
			_position = new Point(0, 0);
			_rotation = 0;
		}
		
		// PUBLIC =================================================================================
		public function init(frames : Object) : void {
			_sprites = frames.sprites;
			
			_spriteRectangle = new Rectangle();
			currentFrame = 0;
		}
		
		public function render() : void {
			if (_canvas && _sprites && !outOfBounds) {
				var canvasBitmapData : BitmapData = _canvas.bitmapData;
				var spriteBitmapData : BitmapData = _sprites[_currentFrame];
				
				if (_rotation != 0) {
					var transformMatrix : Matrix = new Matrix();
					transformMatrix.rotate(_rotation);
					transformMatrix.tx = x;
					transformMatrix.ty = y;
					canvasBitmapData.draw(spriteBitmapData, transformMatrix, null, null, null, true);
				} else {
					canvasBitmapData.copyPixels(spriteBitmapData, _spriteRectangle, _position, null, null, true);
				}
			}
		}
		
		// PRIVATE ================================================================================
		private function get outOfBounds() : Boolean {
			return (x < _canvas.x || x > _canvas.x + _canvas.width || y < _canvas.y || y > _canvas.y + _canvas.height);
		}
		
		// LISTENERS ==============================================================================
	}
}